package piwan.editor;

import piwan.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

import javax.media.opengl.GL;

public class EditMap {
	static final float pointSize = 8.0f;
	static final float lineWidth = 3.0f;
	static final double pointClickRadius = 0.005;
	
	EditMap(MainWindow imain, Camera icam) {
		main = imain;
		cam = icam;
	}
	
	public void saveMap( String fileName ) {
		try {
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter file = new BufferedWriter(fstream);
			// write region types
			file.write(regtypes.size() + "\n");
			for(RegionType r: regtypes)
				r.writeText(file);
			// write resource types
			file.write(restypes.size() + "\n");
			for(ResourceType r: restypes)
				r.writeText(file);
			// write vertices
			file.write(verts.size() + "\n");
			for(int i=0; i<verts.size(); ++i) {
				verts.get(i).writeText(file);
				verts.get(i).saveId = i;
			}
			file.write("\n");
			// write polygons
			//  saveids;
			for(int i=0; i<polys.size(); ++i)
				polys.get(i).saveId = i;

			file.write(polys.size() + "\n");
			for(EditPoly p: polys)
				p.writeText(file);
			file.write("\n");
			file.close();
		} catch(Exception e) {
			e.printStackTrace();
			System.out.println("Couldn't save map.");
		}
	}
	
	public void loadMap( String fileName ) {
		try {
			polys.clear();
			verts.clear();
			restypes.clear();
			regtypes.clear();
			Scanner file = new Scanner(new File(fileName) );
			int nregs, nress;
			
			nregs = file.nextInt();
			for(int i=0;i<nregs;++i) {
				RegionType nt = new RegionType();
				nt.readText(file);
				nt.setId(i);
				regtypes.add( nt );
			}
			
			nress = file.nextInt();
			for(int i=0;i<nress;++i) {
				ResourceType nt = new ResourceType();
				nt.readText(file);
				nt.setId(i);
				restypes.add(nt);
			}	
			
			int nverts = file.nextInt();
			for(int i=0; i<nverts; ++i) {
				EditVertex v = new EditVertex();
				v.readText(file);
				verts.add( v );
			}
			
			int npolys = file.nextInt();
			System.out.println(npolys);
			for(int i=0; i<npolys; ++i) {
				EditPoly p = new EditPoly();
				p.saveId=i;
				polys.add(p);
			}
			
			for(EditPoly p: polys)
				p.readText(file, this);
			
		} catch(Exception e) {
			e.printStackTrace();
			System.out.println("Couldn't save map.");
		}
	}
	
	public void addVertex( EditVertex v ) { verts.add(v); }
	public void addPoly(EditPoly p) { polys.add(p); }
	public void removeVertex( EditVertex v ) { verts.remove(v); }
	public void removePoly(EditPoly p) { polys.remove(p); }
	
	public EditVertex getVertexAt(Vector2 p) {
		for(EditVertex v: verts)
			if( v.getPosition().closerThan(p, pointClickRadius/cam.getZoom()) )
				return v;
		return null;
	}
	
	public EditPoly getPolyAt(Vector2 p) {
		for(EditPoly poly: polys)
			if( poly.isPointInside(p) )
				return poly;
		return null;
	}
	
	public ArrayList<EditPoly> getPolygons() { return polys; }
	
	public void render() {
		GL gl = main.getGL();
		gl.glLineWidth(lineWidth);
		gl.glPointSize(pointSize);
		gl.glDisable(GL.GL_TEXTURE_2D);
		gl.glColor4d(0.5,0.5,0.5,1.0);
		for(EditPoly poly: polys)
			poly.renderLines(gl);
		
		gl.glBegin(GL.GL_POINTS);
		gl.glColor4d(0.0,1.0,0.0,1.0);
		for(EditPoly poly: polys)
			poly.renderPoints(gl);
		gl.glEnd();
	}
	
	public ArrayList<ResourceType> getResourceTypes() { return restypes; }
	public ArrayList<RegionType> getRegionTypes() { return regtypes; }
	public ResourceType getResourceType(int i) { return restypes.get(i); }
	public EditVertex getVertex(int i) { return verts.get(i); }
	public RegionType getRegionType(int i) { return regtypes.get(i); }
	
	private Camera cam = null;
	private MainWindow main = null;
	private ArrayList<EditPoly> polys = new ArrayList<EditPoly>();
	private ArrayList<EditVertex> verts = new ArrayList<EditVertex>();
	private ArrayList<ResourceType> restypes = new ArrayList<ResourceType>();
	private ArrayList<RegionType> regtypes = new ArrayList<RegionType>();	
}
